package me.sdevil507.org.repository;

import me.sdevil507.org.po.OrgPo;
import me.sdevil507.supports.jpa.BaseJpaDslRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * 机构[OrgRepository]
 *
 * @author sdevil507
 * created on 2020-07-13 13:40:22
 */
public interface OrgRepository extends BaseJpaDslRepository<OrgPo, Long> {

    /**
     * 逻辑删除
     *
     * @param id id
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update OrgPo a set a.deleted=true where a.id=:id")
    void logicDelete(@Param("id") Long id);

    /**
     * 设置启用/禁用
     *
     * @param id     id
     * @param enable 启用/禁用
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update OrgPo a set a.enable=:enable where a.id=:id")
    void enable(@Param("id") Long id, @Param("enable") Boolean enable);

    Set<OrgPo> findAllByIdIn(List<Long> orgIds);

    OrgPo findOrgPoByPreDomainName(String preDomainName);

    OrgPo findOrgPoByNameZhAndDeleted(String orgName, boolean deleted);

    @Query(nativeQuery = true, value = "select GROUP_CONCAT(t.d_name) from biz_sysdict as t  where t.d_value in (?1) and t.type_key='ThinkTankClassify' and t.is_del = 0 group by t.type_key")
    String getClassifyName(String[] classify);
}